Store an agent ID in the local storage rather than stashing it in a tag.

Akinori MUSHA 9 years ago
parent
commit
f6d19661ce
2 changed files with 15 additions and 27 deletions
  1. 0 19
      app/models/agents/scheduler_agent.rb
  2. 15 8
      lib/huginn_scheduler.rb

+ 0 - 19
app/models/agents/scheduler_agent.rb

@@ -66,24 +66,5 @@ module Agents
66 66
     before_save do
67 67
       self.memory.delete('scheduled_at') if self.options_changed?
68 68
     end
69
-
70
-    def scheduler_tag
71
-      '%s#%d' % [self.class.name, id]
72
-    end
73
-
74
-    class << self
75
-      def scheduler_tag_to_id(tag)
76
-        case tag
77
-        when /\A#{Regexp.quote(self.name)}\#(\d+)\z/o
78
-          $1.to_i
79
-        end
80
-      end
81
-
82
-      def from_scheduler_tag(tag)
83
-        if id = scheduler_tag_to_id
84
-          find_by(id: id)
85
-        end
86
-      end
87
-    end
88 69
   end
89 70
 end

+ 15 - 8
lib/huginn_scheduler.rb

@@ -4,13 +4,14 @@ class Rufus::Scheduler
4 4
   SCHEDULER_AGENT_TAG = Agents::SchedulerAgent.name
5 5
 
6 6
   class Job
7
-    # Extract an ID of SchedulerAgent if a matching tag is found.
7
+    # Store an ID of SchedulerAgent in this job.
8
+    def scheduler_agent_id= id
9
+      self[:scheduler_agent_id] = id
10
+    end
11
+
12
+    # Extract an ID of SchedulerAgent if any.
8 13
     def scheduler_agent_id
9
-      tags.each { |tag|
10
-        if agent_id = Agents::SchedulerAgent.scheduler_tag_to_id(tag)
11
-          return agent_id
12
-        end
13
-      }
14
+      self[:scheduler_agent_id]
14 15
     end
15 16
 
16 17
     # Return a SchedulerAgent tied to this job.  Return nil if it is
@@ -29,7 +30,9 @@ class Rufus::Scheduler
29 30
 
30 31
   # Get a job tied to a given SchedulerAgent
31 32
   def scheduler_agent_job(agent)
32
-    jobs(tags: [SCHEDULER_AGENT_TAG, agent.scheduler_tag]).first
33
+    scheduler_agent_jobs.find { |job|
34
+      job[:scheduler_agent_id] == agent.id
35
+    }
33 36
   end
34 37
 
35 38
   # Schedule or reschedule a job for a given SchedulerAgent and return
@@ -52,7 +55,11 @@ class Rufus::Scheduler
52 55
         puts "Scheduling SchedulerAgent##{agent.id}"
53 56
       end
54 57
 
55
-      job = schedule_cron agent.options['schedule'], tags: [SCHEDULER_AGENT_TAG, agent.scheduler_tag] do |job|
58
+      agent_id = agent.id
59
+
60
+      job = schedule_cron agent.options['schedule'], tag: SCHEDULER_AGENT_TAG do |job|
61
+        job.scheduler_agent_id = agent_id
62
+
56 63
         if scheduler_agent = job.scheduler_agent
57 64
           scheduler_agent.check!
58 65
         else